package test;

public class SwapOnStack {

    // 通过 jps 查看Class的Main进程的PID
    // 通过 jmap -histo [pid] 查看java堆上的对象分布情况
    // 开启逃逸分析：-Xmx4G -Xms4G -XX:+DoEscapeAnalysis -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError
    // 关闭逃逸分析：-Xmx4G -Xms4G -XX:-DoEscapeAnalysis -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError
    public static void main(String[] args) {

        long a1 = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            alloc();
        }

        // 查看执行时间
        long a2 = System.currentTimeMillis();
        System.out.println("cost " + (a2 - a1) + " ms");

        // 为了方便查看堆内存中对象个数，线程sleep
        try {
            Thread.sleep(100000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

    }

    private static void alloc() {
        User user = new User();
    }

    static class User {
    }

}
